<?php
namespace src;
use classlib\acl;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Cuser extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$list  = C\model::load('main.sys.role')->get_list(0, -1);
		if ($list) {
			foreach ($list as $key=>$item) {
				$list[$key] = array('inputValue' => $item['id'], 'boxLabel' => $item['name'],
					'name'=> 'roles['.$item['id'].']');
			}
		}
		$dlang = lang::inst('dd');
		$view->assign_by_ref('roles', $list);
		$view->assign('status', $dlang->get('sh0001'));
		$view->assign('sex', $dlang->get('sh0003'));
		$view->assign('isdup', $dlang->get('sh0002'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('core/user.ext.js');
	}
	
	/**
	 * @desc    新增以及编辑会员资料信息
	 * @doc     编辑资料
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$user   = array('stime'=>C\config::$nowtime);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$id     = (int)input::get_post('id');
		try {
			$user['account']  = $filter->reset('account')->required()->alpha()->maxlen(64)->run();
			$user['nickname'] = $filter->reset('nickname')->required()->lbetween(1, 64)->run();
			$user['email']    = $filter->reset('email')->required()->email()->run();
			$user['mobile']   = $filter->reset('mobile')->required()->mobile()->run();
			if ($id < 1 || input::get_post('loginpw')) {
				$user['loginpw'] = $filter->reset('loginpw')->nospace()->minlen(6)->run();
				$user['loginpw'] = acl::inst()->crypt($user['loginpw']);
			}
			$acl = acl::inst();
			$res = \src\logic\upfile::inst($acl)->post('avtar');
			if ($res) {
				$user['avtar'] = $res['url'];
			}
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		//验证用户名是否存在
		$where = array('account'=>$user['account']);
		$esid  = C\model::load('main.sys.user')->isexist($where);
		if ($esid && $esid != $id) {
			$view->assign_error('account', $lang->get('common', '40026'));
		}
		$user['status']   = (int)input::post('status');
		$user['isdup']    = (int)input::post('isdup');
		$user['sex']      = (int)input::post('sex');
		$expire  		  = input::post('expire');
		$user['expire']	  = ($expire)? strtotime($expire):0;
		$user['orgid']    = (int)input::post('orgid');
		$org  = C\model::load('main.sys.organize')->get_one($user['orgid']);
		if (empty($org)) {
			$view->assign_error('orgid', $lang->get('common', '40027'));
		}
		$user['english']  = \classlib\cnspell::get_first_spell($user['nickname']);
		if ($id < 1) {
			$user['regtime']   = C\config::$nowtime;
			$user['regip']     = C\get_ip();
			$id = C\model::load('main.sys.user')->new_one($user);
		} else {
			C\model::load('main.sys.user')->save($id, $user);
			Cadmin::remove_access($id);
		}
		//重新记录用户的权限资料信息
		Cadmin::add_user_access($id, input::post('roles'), input::post('access'));
		$view->display();
	}
	
	/**
	 * 获取用户资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$account= input::get_post('account');
		if ($account && !empty($account)) {
			$where[] = array('account', '%'.$account.'%', \classlib\orm::OP_LIKE);
		}
		$status = input::get_post('status');
		if (is_numeric($status) && $status >= 0) {
			$where['status'] = $status;
		}
		$sdate  = input::get_post('sdate');
		if ($sdate) {
			$where[] = array('expire', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::get_post('edate');
		if ($edate) {
			$where[] = array('expire', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orgid  = input::get_post('orgid');
		if(is_numeric($orgid)) {
			$where['orgid'] = $orgid;
		}
		$orm    = C\model::load('main.sys.user');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$expire = C\config::$nowtime + 3600*24*365*4;
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			$org    = C\model::load('main.sys.organize');
			foreach ($list as &$item) {
				if ($item['regtime'] < 1) {
					$item['regtime']= C\config::$nowtime;
				}
				$item['regtime']    = date('Y-m-d H:i', $item['regtime']);
				if ($item['expire'] < 1) {
					$item['expire'] = $expire;
				}
				$item['expire']     = date('Y-m-d', $item['expire']);
				$item['stime']      = date('Y-m-d H:i:s', $item['stime']);
				$orm = $org->get_one($item['orgid']);
				$item['org']        = ($org)? $orm['name']:'';
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * 获取用户角色以及个性权限信息
	 * Enter description here ...
	 */
	public function doaccess() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$userid = (int)input::get_post('id');
		$access = Cadmin::get_role_access($userid, true);
		$view->assign_by_ref('roles', $access['roles']);
		$view->assign_by_ref('access', $access['access']);
		$view->display();
	}
}